<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="采集项名称" prop="collectItemName">
        <el-input
          v-model="queryParams.collectItemName"
          placeholder="请输入采集项名称"
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采集项类型" prop="collectDataType">
        <el-select
          v-model="queryParams.collectDataType"
          filterable
          placeholder="请选择采集项类型"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="item in collectDataTypeOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          >
            <span style="float: left">{{ item.dictValue }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.dictLabel }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['client:mesQualityItems:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['client:mesQualityItems:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['client:mesQualityItems:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['client:mesQualityItems:export']"
        >导出</el-button>
      </el-col> -->
	    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="itemsList"
      @selection-change="handleSelectionChange"
      style="width: 100%"
    >
      <el-table-column type="selection" width="60" align="center" />
      <el-table-column label="采集项ID" align="center" prop="collectItemId" v-if="false" />
      <el-table-column label="采集项名称" align="center" prop="collectItemName" />
      <el-table-column label="采集项单位" align="center" prop="uomCode" />
      <el-table-column label="采集项类型" align="center" prop="collectDataType" />
      <el-table-column label="采集项描述" align="center" prop="collectItemDesc" />
      <el-table-column label="产线" align="center" prop="lineId" />
      <el-table-column label="工位" align="center" prop="stationCode" />
      <el-table-column label="数据类型" align="center" prop="dataType" :formatter="typeFormat" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            style="margin-right: 9px"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['client:mesQualityItems:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['client:mesQualityItems:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改质量数据采集项定义对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="760px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="采集项名称" prop="collectItemName">
              <el-input
                v-model="form.collectItemName"
                placeholder="请输入采集项名称"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="采集项类型" prop="collectDataType">
              <el-select
                v-model="form.collectDataType"
                filterable
                placeholder="请选择采集项类型"
                style="width: 240px"
              >
                <el-option
                  v-for="item in collectDataTypeOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="采集项单位" prop="uomCode">
              <el-input
                v-model="form.uomCode"
                placeholder="请输入采集项单位"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="采集项描述" prop="collectItemDesc">
              <el-input
                v-model="form.collectItemDesc"
                placeholder="请输入采集项描述"
                style="width: 240px"
              />
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="产线" prop="lineId">
              <el-select
                v-model="form.lineId"
                placeholder="请选择产线"
                style="width: 240px"
                filterable
              >
                <el-option
                  v-for="item in lineCodeOptions"
                  :key="item.shopCode"
                  :label="item.shopName"
                  :value="item.shopCode"
                >
                  <span style="float: left">{{ item.shopCode }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.shopName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="工位" prop="stationCode">
              <el-select
                v-model="form.stationCode"
                placeholder="请选择工位"
                style="width: 240px"
                filterable
              >
                <el-option
                  v-for="item in workShipOptions"
                  :key="item.shopCode"
                  :label="item.shopName"
                  :value="item.shopCode"
                >
                  <span style="float: left">{{ item.shopCode }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.shopName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :lg="12">
            <el-form-item label="设备" prop="equipmentId">
              <el-input
                v-model="form.equipmentId"
                placeholder="请输入设备"
                style="width: 240px"
              />
            </el-form-item>
          </el-col> -->
          <el-col :lg="12">
            <el-form-item label="数据类型" prop="dataType">
              <el-select
                v-model="form.dataType"
                filterable
                placeholder="请选择数据类型"
                style="width: 240px"
              >
                <el-option
                  v-for="item in dataTypeOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="12">
            <el-form-item label="状态" style="height: 38px">
              <el-radio-group v-model="form.status" style="width: 240px">
                <el-radio label="0">启用</el-radio>
                <el-radio label="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listItems,
    getItems,
    delItems,
    addItems,
    updateItems,
    exportItems
  } from "@/api/client/items";
  import { getShopByType } from "@/api/base/shop";

  export default {
    name: "mesQualityItems",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 采集项类型下拉框
        collectDataTypeOptions: [],
        // 数据类型下拉框
        dataTypeOptions: [],
        // 产线编码字典
        lineCodeOptions: [],
        // 产线编码字典
        workShipOptions: [],
        // 质量数据采集项定义表格数据
        itemsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          collectItemName: undefined,
          collectDataType: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          collectItemName: [{ required: true, message: "采集项名称不能为空", trigger: "blur" }],
          collectDataType: [{ required: true, message: "采集项类型不能为空", trigger: "blur" }],
          uomCode: [{ required: true, message: "采集项单位不能为空", trigger: "blur" }],
          lineId: [{ required: true, message: "产线不能为空", trigger: "blur" } ],
          stationCode: [{ required: true, message: "工位不能为空", trigger: "blur" }]
        }
      };
    },
    created() {
      this.getList();
      this.getLineData();
      this.getWorkData();
      this.getDicts("collect_data_type").then((response) => {
        this.collectDataTypeOptions = response.data;
      });
      this.getDicts("data_type").then((response) => {
        this.dataTypeOptions = response.data;
      });
    },
    methods: {
      /** 查询质量数据采集项定义列表 */
      getList() {
        this.loading = true;
        listItems(this.queryParams).then(response => {
          this.itemsList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 产线
      getLineData() {
        getShopByType(3).then((res) => {
          this.lineCodeOptions = res.data;
        })
      },
      // 工位
      getWorkData() {
        getShopByType(4).then((res) => {
          this.workShipOptions = res.data;
        })
      },
      // 类型字典翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.dataTypeOptions, row.dataType);
      },
      // 状态字典翻译
      statusFormat(row, column) {
        let str = '';
        switch(row.status) {
          case '0':
            str = '启用'
            break;
          case '1':
            str = '停用'
            break;
          default: console.log('no default')
        }
        return str;
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          status: '0',
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.collectItemId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加质量数据采集项定义";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const collectItemId = row.collectItemId || this.ids
        getItems(collectItemId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改质量数据采集项定义";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.collectItemId != null) {
              updateItems(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addItems(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const collectItemIds = row.collectItemId || this.ids;
        this.$confirm('是否确认删除质量数据采集项定义编号为"' + collectItemIds + '"的数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return delItems(collectItemIds);
          }).then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有质量数据采集项定义数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return exportItems(queryParams);
          }).then(response => {
            this.download(response.msg);
          }).catch(function() {});
      }
    }
  };
</script>
